import { IsString, IsOptional, IsNumber, IsUrl, Length, Min, Max, Matches, IsBoolean, IsInt } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Transform, Type } from 'class-transformer';

export class CreateTokenDto {
  @ApiPropertyOptional({
    description: 'Token mint address on Solana (optional - will be generated if not provided)',
    example: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
  })
  @IsOptional()
  @IsString()
  @Length(32, 44)
  mint?: string;

  @ApiProperty({
    description: 'Token name',
    example: 'c4pad Token',
  })
  @IsString()
  @Length(1, 100)
  name: string;

  @ApiProperty({
    description: 'Token symbol',
    example: 'C4PAD',
  })
  @IsString()
  @Length(1, 20)
  symbol: string;

  @ApiProperty({
    description: 'Token decimals (0-9, commonly 6 or 9)',
    example: 9,
    minimum: 0,
    maximum: 9,
  })
  @IsNumber()
  @IsInt()
  @Min(0)
  @Max(9)
  @Type(() => Number)
  decimals: number;

  @ApiProperty({
    description: 'Token total supply',
    example: '1000000000',
  })
  @IsString()
  @Matches(/^\d+$/, { message: 'supply must be a valid number string' })
  supply: string;

  @ApiPropertyOptional({
    description: 'Token description',
    example: 'A revolutionary meme token on Solana',
  })
  @IsOptional()
  @IsString()
  @Length(0, 1000)
  description?: string;

  @ApiPropertyOptional({
    description: 'Token logo/image URL or base64 data',
    example: 'https://example.com/token-image.png',
  })
  @IsOptional()
  @IsString()
  image?: string;

  @ApiProperty({
    description: 'Creator wallet address',
    example: 'DqeJKgY7z8pFgKKBH8p1QKpHpbQ6YvYvKgSF8vxP8zxP',
  })
  @IsString()
  @Length(32, 44)
  creator: string;

  @ApiPropertyOptional({
    description: 'Payment transaction signature for 0.2 SOL fee',
    example: '5VqE2QKjbzRh3mKJKB7v7F8G7F8G7F8G7F8G7F8G7F8G7F8G7F8G7F8G7F8G7F8G7F8G7F8G7F8G',
  })
  @IsOptional()
  @IsString()
  @Length(80, 90)
  paymentSignature?: string;

  @ApiPropertyOptional({
    description: 'Whether this token is created with bonding curve mechanism',
    example: true,
  })
  @IsOptional()
  @IsBoolean()
  @Transform(({ value }) => value === 'true' || value === true)
  isBondingCurve?: boolean;

  @ApiPropertyOptional({
    description: 'Whether this token has graduated from bonding curve',
    example: false,
  })
  @IsOptional()
  @IsBoolean()
  @Transform(({ value }) => value === 'true' || value === true)
  isGraduated?: boolean;

  // 靓号地址相关字段
  @ApiPropertyOptional({
    description: 'Enable pretty address generation',
    example: false,
  })
  @IsOptional()
  @IsBoolean()
  @Transform(({ value }) => value === 'true' || value === true)
  enablePrettyAddress?: boolean;

  @ApiPropertyOptional({
    description: 'Pretty address prefix',
    example: '123',
  })
  @IsOptional()
  @IsString()
  @Length(0, 10)
  prettyPrefix?: string;

  @ApiPropertyOptional({
    description: 'Pretty address suffix',
    example: '999',
  })
  @IsOptional()
  @IsString()
  @Length(0, 10)
  prettySuffix?: string;

  @ApiPropertyOptional({
    description: 'Case sensitive for pretty address',
    example: false,
  })
  @IsOptional()
  @IsBoolean()
  @Transform(({ value }) => value === 'true' || value === true)
  caseSensitive?: boolean;

  @ApiPropertyOptional({
    description: 'Number of threads for pretty address generation',
    example: 1,
    minimum: 1,
    maximum: 16,
  })
  @IsOptional()
  @IsNumber()
  @IsInt()
  @Min(1)
  @Max(16)
  @Type(() => Number)
  threads?: number;

  // 社交链接字段
  @ApiPropertyOptional({
    description: 'Official website URL',
    example: 'https://example.com',
  })
  @IsOptional()
  @IsUrl({}, { message: 'website must be a valid URL' })
  website?: string;

  @ApiPropertyOptional({
    description: 'Twitter/X profile URL',
    example: 'https://twitter.com/example',
  })
  @IsOptional()
  @IsUrl({}, { message: 'twitter must be a valid URL' })
  twitter?: string;

  @ApiPropertyOptional({
    description: 'Telegram group/channel URL',
    example: 'https://t.me/example',
  })
  @IsOptional()
  @IsUrl({}, { message: 'telegram must be a valid URL' })
  telegram?: string;

  @ApiPropertyOptional({
    description: 'Discord server URL',
    example: 'https://discord.gg/example',
  })
  @IsOptional()
  @IsUrl({}, { message: 'discord must be a valid URL' })
  discord?: string;

  // 权限控制字段
  @ApiPropertyOptional({
    description: 'Revoke update authority (metadata modification)',
    example: false,
  })
  @IsOptional()
  @IsBoolean()
  @Transform(({ value }) => value === 'true' || value === true)
  revokeUpdateAuthority?: boolean;

  @ApiPropertyOptional({
    description: 'Revoke freeze authority',
    example: false,
  })
  @IsOptional()
  @IsBoolean()
  @Transform(({ value }) => value === 'true' || value === true)
  revokeFreezeAuthority?: boolean;

  @ApiPropertyOptional({
    description: 'Revoke mint authority (no more tokens can be minted)',
    example: false,
  })
  @IsOptional()
  @IsBoolean()
  @Transform(({ value }) => value === 'true' || value === true)
  revokeMintAuthority?: boolean;
}

export class UpdateTokenDto {
  @ApiPropertyOptional({
    description: 'Token name',
    example: 'Updated Ignite Token',
  })
  @IsOptional()
  @IsString()
  @Length(1, 100)
  name?: string;

  @ApiPropertyOptional({
    description: 'Token symbol',
    example: 'IGNITE2',
  })
  @IsOptional()
  @IsString()
  @Length(1, 20)
  symbol?: string;

  @ApiPropertyOptional({
    description: 'Token description',
    example: 'Updated description for the token',
  })
  @IsOptional()
  @IsString()
  @Length(0, 1000)
  description?: string;

  @ApiPropertyOptional({
    description: 'Token image URL',
    example: 'https://example.com/new-token-image.png',
  })
  @IsOptional()
  @Matches(/^https?:\/\/.+\.(jpg|jpeg|png|gif)$/i, { message: 'image must be a valid image URL' })
  image?: string;

  @ApiPropertyOptional({
    description: 'Token price in USD',
    example: 0.001,
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  price?: number;

  @ApiPropertyOptional({
    description: 'Market capitalization in USD',
    example: 1000000,
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  marketCap?: number;

  @ApiPropertyOptional({
    description: 'Number of token holders',
    example: 1000,
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  holders?: number;
}
